package org.thunms.schedule.quartz.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thunms.framework.controller.ControllerDefault;
import org.thunms.framework.model.DataGrid;
import org.thunms.framework.model.DataGridJson;
import org.thunms.framework.model.Json;
import org.thunms.schedule.quartz.model.QrtzTriggers;
import org.thunms.schedule.quartz.service.QrtzTriggersService;

/**
 * 工作任务引擎
 * 任务管理子系统
 * 任务运行监控
 */
@Controller
@RequestMapping("/schedule/quartz/qrtzTriggers")
public class QrtzTriggersController extends ControllerDefault {
	private static final long serialVersionUID = 4780087695307228907L;
	private static final Logger logger =LoggerFactory.getLogger(QrtzTriggersController.class);

	public QrtzTriggersController() {
		super("schedule", "quartz", "qrtzTriggers");
		logger.debug("执行模块初始化"+this.getRerutnURL(""));
	}
	
	@Autowired
	private QrtzTriggersService qrtzTriggersService;
	
	@Override
	protected void listPageCallback(HttpServletRequest request,	HttpServletResponse response, Model model) {
		//triggerGroup
		model.addAttribute("triggerGroup", request.getParameter("triggerGroup"));
	}
	
	@RequestMapping(value = "/dataGridList")
	@ResponseBody
	public DataGridJson<QrtzTriggers> dataGridList(DataGrid dg, QrtzTriggers mode) {
		return this.qrtzTriggersService.getDataGridList(dg, mode);
	}
	
	@RequestMapping(value="/advancedAdd")
	public String advancedAdd(){
		return this.getRerutnURL("advanced_add");
	}
	
	/**
	 * 高级任务添加
	 * @return
	 */
	@RequestMapping(value="/sdvancedAddSave")
	@ResponseBody
	public Json sdvancedAddSave(String triggerName,String triggerGroup,String cronExpression){
		Json msg=new Json();
		if(qrtzTriggersService.sdvancedAddSave(triggerName,triggerGroup, cronExpression)){
			msg.setSuccess(true);
			msg.setMsg("任务执行规则重设成功");	
		}else{
			msg.setSuccess(false);
			msg.setMsg("任务执行规则重设失败");	
		}
		
		return msg;
	}
	
	/**
	 * 启动所有任务
	 */
	@RequestMapping(value="/startJobsAll")
	@ResponseBody
	public Json startJobsAll(){
		Json msg=new Json();
		this.qrtzTriggersService.startJobsAll();
		msg.setSuccess(true);
		msg.setMsg("启动所有任务！");
		
		return msg;
	}
	/**
	 * 停止所有任务
	 */
	@RequestMapping(value="/shutdownJobsAll")
	@ResponseBody
	public Json shutdownJobsAll(){
		Json msg=new Json();
		this.qrtzTriggersService.shutdownJobsAll();
		msg.setSuccess(true);
		msg.setMsg("停止所有任务！");
		return msg;
	}
	
	
	
	/**
	 * 任务暂停所有任务
	 */
	@RequestMapping(value="/suspendAll")
	@ResponseBody
	public Json suspendAll(){
		Json msg=new Json();
		this.qrtzTriggersService.suspendAll();
		msg.setSuccess(true);
		msg.setMsg("任务暂停成功！");
		
		return msg;
	}
	/**
	 * 任务启动恢复所有任务
	 */
	@RequestMapping(value="/recoveryAll")
	@ResponseBody
	public Json recoveryAll(){
		Json msg=new Json();
		this.qrtzTriggersService.recoveryAll();
		msg.setSuccess(true);
		msg.setMsg("任务启动恢复成功！");
		return msg;
	}
	/**
	 * 任务暂停所有任务
	 */
	@RequestMapping(value="/suspend")
	@ResponseBody
	public Json suspend(String triggerName,String triggerGroup){
		Json msg=new Json();
		this.qrtzTriggersService.suspend(triggerName, triggerGroup);
		msg.setSuccess(true);
		msg.setMsg("任务暂停成功！");
		
		return msg;
	}
	/**
	 * 任务启动恢复所有任务
	 */
	@RequestMapping(value="/recovery")
	@ResponseBody
	public Json recovery(String triggerName,String triggerGroup){
		Json msg=new Json();
		this.qrtzTriggersService.recovery(triggerName, triggerGroup);
		msg.setSuccess(true);
		msg.setMsg("任务启动恢复成功！");
		return msg;
	}
	/**
	 * 删除
	 */
	@RequestMapping(value="/deleteJob")
	@ResponseBody
	public Json deleteJob(String triggerName,String triggerGroup){
		Json msg=new Json();
		this.qrtzTriggersService.deleteJob(triggerName, triggerGroup);
		msg.setSuccess(true);
		msg.setMsg("任务删除成功！");
		return msg;
	}
	/**
	 * @throws IOException 
	 * 
	 */
	@RequestMapping(value="/simulatio")
	public void simulatio(String triggerName,String triggerGroup,HttpServletRequest request, HttpServletResponse response) throws IOException{
		response.getWriter().print(this.qrtzTriggersService.simulatio(triggerName, triggerGroup));
	}
	
}
